package models

import (
	"context"
	"go-chain-data/global"
	"go.mongodb.org/mongo-driver/bson"
)

type SwapInfo struct {
	Address       string `bson:"address" `      // 交易对智能合约地址
	Token0Address string `bson:"token0Address"` // 交易对中第一个代币的地址
	Token1Address string `bson:"token1Address"` // 交易对中第二个代币的地址
	Token0Name    string `bson:"token0Name"`    // 交易对中第一个代币的名称
	Token1Name    string `bson:"token1Name"`    // 交易对中第二个代币的名称
	Factory       string `bson:"factory"`       // 交易对所属的工厂地址
	ChainId       uint   `bson:"chainId"`
	ChainName     string `bson:"chainName"`
}

func GetSwapInfoByAddress(address string) (SwapInfo, error) {
	var swapInfo SwapInfo
	swapInfoCollection := global.DBEngine.Database(global.DbConfig.DbName).Collection("swap_info")
	filter := bson.D{{"address", address}}
	err := swapInfoCollection.FindOne(context.Background(), filter).Decode(&swapInfo)
	return swapInfo, err
}

func CreateSwapInfo(swapInfo *SwapInfo) error {
	swapInfoCollection := global.DBEngine.Database(global.DbConfig.DbName).Collection("swap_info")
	_, err := swapInfoCollection.InsertOne(context.Background(), swapInfo)
	return err
}
